//
//  Util.cpp
//  StarBugs
//
//  Created by ?? ? on 12/23/11.
//  Copyright (c) 2011 antisword@playorca.com. All rights reserved.
//

#include "Util/Util.h"
#include "Box2D.h"
using namespace cocos2d;

CCRect Util::makeRectWithLabel(CCLabelTTF *label)
{
    CCPoint pt;
    pt.x = label->getPosition().x - (label->getContentSize().width * label->getScale()) * label->getAnchorPoint().x;
    pt.y = label->getPosition().y - (label->getContentSize().height * label->getScale()) * label->getAnchorPoint().y;
    
	return CCRect(pt.x,pt.y,
                  (label->getContentSize().width * label->getScale()),
                  (label->getContentSize().height * label->getScale()));
}

CCRect Util::makeRectWithSprite(CCSprite * mySprite)
{
    CCPoint pt;
//    CCPoint worldPt = mySprite->convertToWorldSpace(mySprite->getPosition());
    
    pt.x = mySprite->getPosition().x - (mySprite->getContentSize().width * mySprite->getScaleX()) * mySprite->getAnchorPoint().x;
    pt.y = mySprite->getPosition().y - (mySprite->getContentSize().height * mySprite->getScaleY()) * mySprite->getAnchorPoint().y;
    
	return CCRect(pt.x,pt.y,
                      (mySprite->getContentSize().width * mySprite->getScaleX()),
                      (mySprite->getContentSize().height * mySprite->getScaleY()));
}
    
CCPoint Util::makePointAboutAnchor(CCSize size, CCPoint anchor)
{
    return CCPoint(size.width * anchor.x, size.height * anchor.y);
}

CCRect Util::makeRectWithSpriteWorldSpace(cocos2d::CCSprite *mySprite, cocos2d::CCNode* node)
{
    CCPoint pt;
    CCPoint worldPt = node->convertToWorldSpace(mySprite->getPosition());
    
    pt.x = worldPt.x - (mySprite->getContentSize().width * mySprite->getScaleX()) * mySprite->getAnchorPoint().x;
    pt.y = worldPt.y - (mySprite->getContentSize().height * mySprite->getScaleY()) * mySprite->getAnchorPoint().y;
 
    
    
	return CCRect(pt.x,pt.y,
                  (mySprite->getContentSize().width * abs(mySprite->getScaleX())),
                  (mySprite->getContentSize().height * abs(mySprite->getScaleY())));
}

CCPoint Util::centerPointWithSpriteWorldSpace(cocos2d::CCSprite *mySprite, cocos2d::CCNode *node)
{
    CCPoint pt;
    CCPoint worldPt = node->convertToWorldSpace(mySprite->getPosition());
    
    pt.x = worldPt.x - (mySprite->getContentSize().width * abs(mySprite->getScaleX())) * mySprite->getAnchorPoint().x;
    pt.y = worldPt.y - (mySprite->getContentSize().height * abs(mySprite->getScaleY())) * mySprite->getAnchorPoint().y;
    
    return CCPoint(pt.x + (mySprite->getContentSize().width/2 * abs(mySprite->getScaleX())), 
                   pt.y + (mySprite->getContentSize().height/2 * abs(mySprite->getScaleY())));
}

void Util::makeLineWithSprite(cocos2d::CCSprite *sprite, cocos2d::CCPoint first, cocos2d::CCPoint second)
{
    sprite->setAnchorPoint(ccp(0, 0.5));
    sprite->setPosition(first);
//    sprite->setRotation(ccpAngle(first, second) * 180 / M_PI - 180);
//    sprite->setRotation(atan2f(first.x - second.x, first.y - second.y) * 180 / b2_pi);
    sprite->setRotation(atan2f(second.x - first.x, second.y - first.y) * 180 / b2_pi - 90);
    sprite->setScaleX(ccpDistance(first, second) / sprite->getContentSize().width);
}

//void Util::printCurrentTime(int num)
//{
//    struct timeval currentTime;
//    gettimeofday(&currentTime, (void *)NULL);
//    static int timess;
//    
//    if(num == 1)
//        timess = (currentTime.tv_sec*1000 + currentTime.tv_usec/1000);
//    else
//        CCLog("currentTime( %d)",(currentTime.tv_sec*1000 + currentTime.tv_usec/1000) - timess);
//}

//CCPoint Util::rotatePointAboutPoint(cocos2d::CCPoint targetPt, cocos2d::CCPoint anchorPt, float angle)
//{   
//    ccpRotateByAngle(targetPt, anchorPt, angle);
//}
